<?php
namespace app\admin\controller;
use think\Request;
use think\Controller;
use think\Db;
use app\admin\model\Shopemail;
use app\admin\controller\Base;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class Email extends Base
{
    public function sendmail()
    {
        return $this->fetch();
    }

    public function send(){
        
        $goodsurl=input('post.goodsurl');
        $senduser =input('post.senduser');
        $content=input('post.content');
        $time=time();
       
        $makeuser=$_SESSION['username'];
        $data=[
            'goodsurl'=>$goodsurl,
            'senduser'=>$senduser,
            'content'=>$content,
            'addtime'=>$time,
            'makeuser'=>$makeuser
        ];
        $emailinfo = Db("emaillog")->insert($data);
        $send=new Shopemail();
        //$senduser='2972409305@qq.com';
        if(empty($goodsurl)){
            echo "<script>alert('对不起链接地址不正确');location.href=history.go(-1)</script>";die;
        }
        if(empty($senduser) || $senduser=='0'){
            echo "<script>alert('对不起请选择发送人员');location.href=history.go(-1)</script>";die;
        }
        if(empty($content)){
            echo "<script>alert('对不起内容不正确');location.href=history.go(-1)</script>";die;
        }

        if($senduser=='1'){
            $emailinfo = Db::query("select user_email from user ");
        }
        if($senduser=='2'){
            $emailinfo = Db::query("select member_email from member ");
        }
        
        foreach ($emailinfo as $key => $value) {
            $res= $send->index($goodsurl,$value['user_email'],$content);
        }
        echo 1;
       
      
    }

    public function sendlog(){

        $emailinfo = Db::query("select * from emaillog where status='0' ");
        return $this->fetch('sendlog',['data'=>$emailinfo]);
    }
   
}